#include "RiskServer.h"
#include "Main/global.h"
#include <QCryptographicHash>
#include <QDebug>
#include "Main/constant.h"
#include "Msg/Rsp/rspbase.h"
#include "Msg/msgbase.h"

RiskServer::RiskServer()
{
    m_nCurSevPos = 0;
    m_nRiskWatchMode = -1;
}

void RiskServer::InitServerAddr(HashtableMsg htm_server_list)
{
    m_server_list = htm_server_list;
}

bool RiskServer::BranchRiskStatQry(const QString &sBranchID, ArrayListMsg &alm, QString &qsErrMsg)
{
    MsgBase req3306;
    req3306.AddPara("branch_id", sBranchID);
    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3306");
    std::string sRsp;
    if (TransForNormal(req3306.ToString(), sRsp, qsErrMsg))
    {
        RspBase rspMsg;
        rspMsg.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
        alm.Parse(rspMsg.GetQStringPara("rtn_result"));
        QString statAgentMode = rspMsg.GetQStringPara("indirect");
        if (statAgentMode.length() > 0)
            m_nRiskWatchMode = statAgentMode.toInt();
        return true;
    }

    return false;
}

bool RiskServer::RiskCustListQry(const QString qsCondition, ArrayListMsg &alm, QString &qsErrMsg)
{
    MsgBase req3062;
    req3062.AddPara("oper_flag", 2);
    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3062");
    std::string sRsp;
    if (TransForNormal(req3062.ToString()+qsCondition.toStdString(), sRsp, qsErrMsg))
    {
        RspBase rspMsg;
        rspMsg.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
        alm.Parse(rspMsg.GetQStringPara("risk_result"));
        return true;
    }

    return false;
}

bool RiskServer::CustDetailQry(const QString acctNo,RspInterface* pRsp, QString &qsErrMsg)
{
    MsgBase req3063;
    req3063.AddPara("oper_flag", 1);
    req3063.AddPara("acct_no", acctNo);
    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3063");
    std::string sRsp;
    if (TransForNormal(req3063.ToString(), sRsp, qsErrMsg))
    {
        pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
        ArrayListMsg almPosi,almStore;
        almPosi.Parse(pRsp->m_rsp.GetQStringPara("alm_posi_info"));
        pRsp->m_alms["alm_posi_info"] = almPosi;
        almStore.Parse(pRsp->m_rsp.GetQStringPara("alm_stor_info"));
        pRsp->m_alms["alm_stor_info"] = almStore;
        return true;
    }

    return false;
}

bool RiskServer::SendForceOrder(const QString acctNo, const QString fcOrderInfo, RspInterface * pRsp)
{
    MsgBase req3066;
    req3066.AddPara("oper_flag", 1);
    req3066.AddPara("acct_no", acctNo);
    req3066.AddPara("source_falg", 2);
    req3066.AddPara("teller_id", Global::Instance()->GetParaFrom8001("user_id"));
    req3066.AddPara("date", Global::Instance()->GetParaFrom8001("exch_date"));
    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3066");
    std::string sRsp;
    QString qsErrMsg;
    if (TransForNormal(req3066.ToString()+fcOrderInfo.toStdString(), sRsp, qsErrMsg))
    {
        pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
        return true;
    }

    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    return false;
}

bool RiskServer::CancelForceOrder(const QString qsOrderNo, QString &qsRspMsg)
{
    MsgBase req3070;
    req3070.AddPara("oper_flag", 1);
    req3070.AddPara("order_no", qsOrderNo);

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3070");
    std::string sRsp;
    QString qsErrMsg;
    bool bRet = TransForNormal(req3070.ToString(), sRsp, qsErrMsg);
    RspBase rspMsg;
    rspMsg.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    qsRspMsg = rspMsg.GetQStringPara("rsp_msg");

    return bRet;
}

bool RiskServer::TryCalcRiskInfo(const QString acctNo, const QString reqInfo, RspInterface *pRsp)
{
    MsgBase req;
    req.AddPara("oper_flag", 1);
    req.AddPara("acct_no", acctNo);
    req.AddPara("close_para", reqInfo);

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3097");
    std::string sRsp;
    QString qsErrMsg;
    bool bRet = TransForNormal(req.ToString(), sRsp, qsErrMsg);
    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    return bRet;
}

bool RiskServer::QueryPartMatchCovOrder(const QString reqInfo, RspInterface *pRsp)
{
    MsgBase req;
    req.AddPara("oper_flag", 1);

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3065");
    std::string sRsp;
    QString qsErrMsg;
    bool bRet = TransForNormal(req.ToString()+reqInfo.toStdString(), sRsp, qsErrMsg);
    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("f_order_result"));
        pRsp->m_alms["f_order_result"] = alm;
    }
    return bRet;
}

int RiskServer::GetRiskWatchMode()
{
    return m_nRiskWatchMode;
}

bool RiskServer::QueryProdPrice(const QString reqInfo, RspInterface *pRsp)
{
    MsgBase req;
    req.AddPara("oper_flag", 1);

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3098");
    std::string sRsp;
    QString qsErrMsg;
    bool bRet = TransForNormal(req.ToString()+reqInfo.toStdString(), sRsp, qsErrMsg);
    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("quotaion_result"));
        pRsp->m_alms["quotaion_result"] = alm;
    }
    return bRet;
}

bool RiskServer::RiskTestCalc(const QString reqInfo,RspInterface *pRsp, QString &qsErrMsg)
{
    MsgBase req;
    req.AddPara("oper_flag", 1);
    req.AddPara("p_branch_id", Global::Instance()->GetParaFrom8001("branch_id"));

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3093");
    std::string sRsp;
//    QString qsErrMsg;
    bool bRet = TransForNormal(req.ToString()+reqInfo.toLocal8Bit().toStdString(), sRsp, qsErrMsg);
//    RspBase rspMsg;
    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("cal_result"));
        pRsp->m_alms["cal_result"] = alm;
    }
//    pModel->InitAlmData(alm);
    return bRet;
}

bool RiskServer::QueryRiskCustStatics(const QString reqInfo, RspInterface *pRsp, QString &qsErrMsg)
{
    MsgBase req;
    req.AddPara("oper_flag", 1);
//    req.AddPara("p_branch_id", Global::Instance()->GetParaFrom8001("branch_id"));

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3091");
    std::string sRsp;

    bool bRet = TransForNormal(req.ToString()+reqInfo.toLocal8Bit().toStdString(), sRsp, qsErrMsg);

    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("alm_result"));
        pRsp->m_alms["alm_result"] = alm;
    }

    return bRet;
}

bool RiskServer::QueryRiskTip(const QString reqInfo, RspInterface *pRsp, QString &qsErrMsg)
{
    MsgBase req;
    req.AddPara("oper_flag", 1);
//    req.AddPara("p_branch_id", Global::Instance()->GetParaFrom8001("branch_id"));

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3064");
    std::string sRsp;

    bool bRet = TransForNormal(req.ToString()+reqInfo.toLocal8Bit().toStdString(), sRsp, qsErrMsg);

    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("alm_result"));
        pRsp->m_alms["alm_result"] = alm;
    }

    return bRet;
}

bool RiskServer::OperRiskDegree(const QString reqInfo, RspInterface *pRsp, QString &qsErrMsg)
{
    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3081");
    std::string sRsp;

    bool bRet = TransForNormal(reqInfo.toLocal8Bit().toStdString(), sRsp, qsErrMsg);

    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("alm_result"));
        pRsp->m_alms["alm_result"] = alm;
    }

    return bRet;
}

bool RiskServer::CreateBatchForceOrder(const QString reqInfo, RspInterface *pRsp, QString &qsErrMsg)
{
    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "3309");
    std::string sRsp;

    bool bRet = TransForNormal(reqInfo.toLocal8Bit().toStdString(), sRsp, qsErrMsg);

    pRsp->m_rsp.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    if (bRet)
    {
        ArrayListMsg alm;
        alm.Parse(pRsp->m_rsp.GetQStringPara("alm_result"));
        pRsp->m_alms["alm_result"] = alm;
    }

    return bRet;
}

bool RiskServer::GetNetAddress(QString &qsIP, int &nPort)
{
    qsIP = m_server_list.GetQString(m_nCurSevPos, "risk_trans_ip");
    nPort = m_server_list.GetQString(m_nCurSevPos, "risk_trans_port").toInt();
//    qsIP = "127.0.0.1";
//    nPort =63501;
    qDebug()<<"风控："<<qsIP<<":"<<nPort;
    return true;
}

bool RiskServer::TransForNormal(const std::string &sReq, std::string &sRsp, QString &qsErrMsg)
{
    QString qsIP;
    int nPort;
    if (!GetNetAddress(qsIP, nPort))
        return false;

    m_comClient.Init(qsIP, nPort);
    PACKET_INFO pktInfo(CHANNEL_RISK);
    pktInfo.nEncr = 0;
    sprintf(Global::Instance()->gReqHead.term_type,"%s",gc_sTermTypeRisk.c_str());

    int nRet = m_comClient.TransForNormal(pktInfo, sReq, sRsp, qsErrMsg);
    if (nRet != E_TRANS_SUCCESS)
    {
        if (++m_nCurSevPos >= m_server_list.size())
            m_nCurSevPos = 0;
        return false;
    }

    HEADER_RSP rspHead;
    rspHead.DecodeRspHead(sRsp.c_str());
//    qDebug()<< "响应Risk:"<< QString::fromStdString(sRsp);
    if ( 0 == gc_ErrCodeSus.compare(rspHead.rsp_code))
    {
        qsErrMsg = "";
        return true;
    }

    RspBase rspMsg;
    rspMsg.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
    qsErrMsg = rspMsg.GetQStringPara("rsp_msg");
    qDebug()<<"qsErrMsg:"<<qsErrMsg;
    return false;
}
